<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_newcar/model')->方法名();
*/
namespace jy_newcar;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function check_level(){
		return true;
	}
	public function current_level_up_types(){
		return str_array($this->setting['level_up_types']);
	}
	public function upsendlog($order=array(),$status=1){
		$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$order['mid'],
				'orderid'=>$order['id'],
				'workertitle'=>$order['workertitle'],
				'workermobile'=>$order['workermobile'],
				'image'=>$order['image'],
				'price'=>$order['price'],
				'orderstatus'=>$order['status'],
				'status'=>$status,
				'createtime'=>TIMESTAMP,
		);
		$this->insert('sendlog',$data);
		return true;
	}
	public function upsendlog2($sendlog=array()){
		$data=array(
			'workertitle'=>$sendlog['workertitle'],
			'workermobile'=>$sendlog['workermobile'],
			'image'=>$sendlog['image'],
		);
		$this->update('sendlog',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$sendlog['id']));
		return true;
	}
	public function uptuilog($order=array(),$type=0,$status=0){
		$num=pdo_count('jy_newcar_tuilog',array('type'=>$type,'uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']));
		$sn=sprintf("%04d", $num+1);
		$order['paytime']=$order['paytime']?:TIMESTAMP;
		$nowmonth=date('m',$order['paytime']);
		$endtime = mktime(0,0,0,$nowmonth+37,1,date('Y'));
		$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$order['mid'],
				'orderid'=>$order['id'],
				'allqi'=>36,
				'lastqi'=>36,
				'sn'=>$sn,
				'type'=>$type,
				'status'=>$status,
				'endtime'=>$endtime,
				'createtime'=>TIMESTAMP,
		);
		$this->insert('tuilog',$data);
		return $data;
	}
	//购车合伙人推广
	public function gettuiqi0($member=array(),$order=array(),$isaj=false){
		if(!$member['type'] || $member['type']==2){
			$tuilog=pdo_get('jy_newcar_tuilog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid'],'type'=>0,'status'=>1,'num <='=>2));
			$num=pdo_count('jy_newcar_order',array('uniacid'=>$_SESSION['uniacid'],'tuilogid'=>$tuilog['id']));
			if($num>=3){
				pdo_update('jy_newcar_tuilog',array('num'=>3),array('uniacid'=>$_SESSION['uniacid'],'id'=>$tuilog['id']));
				$tuilog=pdo_get('jy_newcar_tuilog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid'],'type'=>0,'status'=>1,'num <='=>2));
			}
			if($tuilog['id'] && $tuilog['endtime']<TIMESTAMP){
				pdo_update('jy_newcar_tuilog',array('status'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$tuilog['id'],'status'=>1));
				$tuilog=pdo_get('jy_newcar_tuilog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid'],'type'=>0,'status'=>1,'num <='=>2,'endtime >'=>TIMESTAMP));
			}
		}
		if($tuilog['id']){
			$num=pdo_count('jy_newcar_order',array('uniacid'=>$_SESSION['uniacid'],'tuilogid'=>$tuilog['id']));
			$tuilog['num']=$num;
			$tuilog['getqikey']=$isaj?$num:$num+1;
			$nowmonth=date('m',$tuilog['starttime']);
			$nowyear=date('Y',$tuilog['starttime']);
			$lastyufee=pdo_get('jy_newcar_yufee',array('uniacid'=>$_SESSION['uniacid'],'tuilogid'=>$tuilog['id']),'','no desc');
			$allqi=$lastyufee['no']?:0;
			$nowqi=0;
			if($allqi){
				for($i=0;$i<$allqi;$i++){
					$yuestart = mktime(0,0,0,$nowmonth+$i,1,date('Y'));
					$yueend = mktime(0,0,0,$nowmonth+$i+1,1,date('Y'));
					if($yueend>TIMESTAMP){
						$nowqi=$i+1;
						break;
					}
				}					
			}else{
				$nowqi=$isaj?1:0;
			}
			$tuilog['nowqi']=$nowqi;
		}
		return $tuilog;
	}
	//购车合伙人推广
	public function getistuiqi($member=array(),$carorder=array()){
		$tuilog=pdo_get('jy_newcar_tuilog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$carorder['tuilogid']));
		if($tuilog['id']){
			$num=pdo_count('jy_newcar_order',array('uniacid'=>$_SESSION['uniacid'],'tuilogid'=>$tuilog['id']));
			$tuilog['num']=$num;
			$tuilog['getqikey']=$num;
			$nowmonth=date('m',$tuilog['starttime']);
			$nowyear=date('Y',$tuilog['starttime']);
			$lastyufee=pdo_get('jy_newcar_yufee',array('uniacid'=>$_SESSION['uniacid'],'tuilogid'=>$tuilog['id']),'','no desc');
			$allqi=$lastyufee['no']?:0;
			$nowqi=0;
			if($allqi){
				for($i=0;$i<$allqi;$i++){
					$yuestart = mktime(0,0,0,$nowmonth+$i,1,date('Y'));
					$yueend = mktime(0,0,0,$nowmonth+$i+1,1,date('Y'));
					if($yueend>TIMESTAMP){
						$nowqi=$i+1;
						break;
					}
				}					
			}else{
				$nowqi=$isaj?1:0;
			}
			$tuilog['nowqi']=$nowqi;
		}
		return $tuilog;
	}
	//提车时才计算：
	public function gettuiqi1($member=array(),$carorder=array(),$isaj=false){
		if($member['type']){
			$tuilog=pdo_get('jy_newcar_tuilog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid'],'type'=>1,'status'=>array(0,1)));
		}
		if($member['type'] && !$tuilog['id'] && !$carorder['tuilogid']){
			$orderid=pdo_getcolumn('jy_weishop_order',array('ordersn'=>$carorder['ordersn'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']),'id');
			$post=array(
				'mid'=>$member['mid'],
				'id'=>$orderid,
			);
			$this->uptuilog($post,1,1);
			$tuilog=pdo_get('jy_newcar_tuilog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid'],'type'=>1,'status'=>array(0,1)));
			if($tuilog['id']){
				pdo_update('jy_newcar_tuilog',array('status'=>1,'starttime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'id'=>$tuilog['id']));
			}
			$tuilog['nowqi']=$isaj?1:0;
		}
		if($tuilog['id']){
			if(!$carorder['tuilogid']){
				pdo_update('jy_newcar_order',array('tuilogid'=>$tuilog['id'],'type'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$carorder['id']));
			}
			$num=pdo_count('jy_newcar_order',array('uniacid'=>$_SESSION['uniacid'],'tuilogid'=>$tuilog['id']));
			if($num>=3){
				pdo_update('jy_newcar_tuilog',array('status'=>2,'succtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'id'=>$tuilog['id'],'status'=>array(0,1)));
			}
			$tuilog['num']=$num;
			$tuilog['getqikey']=$num;
		}
		return $tuilog;
	}
	public function gettuiqi($member=array(),$order=array(),$isaj=false){
		if(!$member['type'] || $member['type']==2){
			$tuilog=pdo_get('jy_newcar_tuilog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid'],'type'=>0,'status'=>array(0,1)));
			if($tuilog['id'] && $tuilog['endtime']<TIMESTAMP){
				pdo_update('jy_newcar_tuilog',array('status'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$tuilog['id'],'status'=>array(0,1)));
				$tuilog='';
			}	
			if($member['type'] && !$tuilog){
				$tuilog=pdo_get('jy_newcar_tuilog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid'],'type'=>1,'status'=>array(0,1)));
			}
		}elseif($member['type']==1){
			$tuilog=pdo_get('jy_newcar_tuilog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid'],'type'=>1,'status'=>array(0,1)));
		}
		if($member['type'] && !$tuilog['id'] && $this->setting['isfee2']){
			$post=array(
				'mid'=>$member['mid'],
				'id'=>$order['id'],
			);
			$this->uptuilog($post,1);
			$tuilog=pdo_get('jy_newcar_tuilog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid'],'type'=>1,'status'=>array(0,1)));
			$tuilog['nowqi']=$isaj?1:0;
		}
		if($tuilog['id']){
			$num=pdo_count('jy_newcar_order',array('uniacid'=>$_SESSION['uniacid'],'tuilogid'=>$tuilog['id']));
			$tuilog['num']=$num;
			$tuilog['getqikey']=$isaj?$num:$num+1;
			$nowmonth=date('m',$tuilog['starttime']);
			$nowyear=date('Y',$tuilog['starttime']);
			$lastyufee=pdo_get('jy_newcar_yufee',array('uniacid'=>$_SESSION['uniacid'],'tuilogid'=>$tuilog['id']),'','no desc');
			$allqi=$lastyufee['no']?:0;
			if($allqi){
				for($i=0;$i<$allqi;$i++){
					$yuestart = mktime(0,0,0,$nowmonth+$i,1,date('Y'));
					$yueend = mktime(0,0,0,$nowmonth+$i+1,1,date('Y'));
					if($yueend>TIMESTAMP){
						$nowqi=$i+1;
						break;
					}
				}
			}else{
				$nowqi=$isaj?1:0;
			}
			$tuilog['nowqi']=$nowqi;
		}
		return $tuilog;
	}
	public function uptuiqi($tuilogid=0){
		$tuilog=pdo_get('jy_newcar_tuilog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$tuilogid,'status'=>array(0,1)));
		if(!$tuilog){
			return '';
		}
		if($tuilog['endtime']<TIMESTAMP){
			pdo_update('jy_newcar_tuilog',array('status'=>3),array('uniacid'=>$_SESSION['uniacid'],'id'=>$tuilogid,'status'=>array(0,1)));	
		}
		$num=pdo_count('jy_newcar_order',array('uniacid'=>$_SESSION['uniacid'],'tuilogid'=>$tuilogid));
		if($num>=3){
			pdo_update('jy_newcar_tuilog',array('status'=>2,'succtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'id'=>$tuilogid,'status'=>array(0,1)));
		}
		return '';
	}
}
?>